home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’95 / Menu Controls / ToolsMenu.cp < prev    next >
Encoding:
Text File  |  1995-06-24  |  15.0 KB  |  500 lines  |  [TEXT/MPS ]

  1. // Copyright © 1994-95 by Apple Computer, Inc. All rights reserved.
  2. // ToolsMenu.cp
  3.  
  4. #ifndef __TOOLSMENU__
  5. #include "ToolsMenu.h"
  6. #endif
  7.  
  8. // MacApp
  9.  
  10. #ifndef __UWINDOW__
  11. #include <UWindow.h>
  12. #endif
  13.  
  14. #ifndef __ULIST__
  15. #include "UList.h"
  16. #endif
  17.  
  18. #ifndef __UMACAPPUTILITIES__
  19. #include <UMacAppUtilities.h>
  20. #endif
  21.  
  22. // DrawShapes
  23.  
  24. #ifndef __USHAPES__
  25. #include "UShapes.h"
  26. #endif
  27.  
  28. //----------------------------------------------------------------------------------------
  29. // Globals
  30.  
  31. TToolsMenu*        gToolsMenu;
  32. TWindow*        gToolsPaletteWindow;
  33.  
  34. //TShape*            gShapesArray[kShapesInPalette];    // prototype shapes for the palette
  35.  
  36. TList*            gProtoShapes = NULL;
  37.  
  38. #define cChangeTool 1014
  39.  
  40. //----------------------------------------------------------------------------------------
  41. // Constants
  42.  
  43. #define kToolsPaletteWidth 41
  44. #define kToolsPaletteHeight 240
  45.  
  46. const short kToolsPaletteRSRCID = 1007;        // Resource id for the floating window with
  47.                                             // tools palette
  48.  
  49. //----------------------------------------------------------------------------------------
  50. // Setter for gShapesArray
  51. #pragma segment ShapeRes
  52.  
  53. void SetShapeInArray(TShape* shape)
  54. {
  55.     if (!gProtoShapes)
  56.     {
  57.         gProtoShapes = NewList();
  58.         FailNIL(gProtoShapes);
  59.     }
  60.     gProtoShapes->InsertLast(shape);
  61. }
  62.  
  63. //----------------------------------------------------------------------------------------
  64. // Accessor for gShapesArray
  65. #pragma segment ShapeRes
  66.  
  67. TShape* GetShapeInArray(short shapeID)
  68. {
  69. #if qRangeCheck
  70.     if ((shapeID < 0) || (gProtoShapes == NULL) || (shapeID >= gProtoShapes->fSize))
  71.     {
  72.         ProgramBreak("GetShapeInArray: shapeID out of range!");
  73.         return NULL;
  74.     }
  75. #endif
  76.  
  77.     return (TShape*) gProtoShapes->At(shapeID + 1);
  78. }
  79.  
  80. //----------------------------------------------------------------------------------------
  81. // Accessor for gShapesArray
  82. #pragma segment ShapeRes
  83.  
  84. short GetNumberOfShapes()
  85. {
  86.     return (short) gProtoShapes->GetSize();
  87. }
  88.  
  89. //========================================================================================
  90. // CLASS TToolsMenu
  91. //========================================================================================
  92. #undef Inherited
  93. #define Inherited TTearOffMenuView
  94.  
  95. #pragma segment AInit
  96. MA_DEFINE_CLASS_M1(TToolsMenu, Inherited);
  97.  
  98. //----------------------------------------------------------------------------------------
  99. // TToolsMenu Constructor
  100. //----------------------------------------------------------------------------------------
  101. #pragma segment AInit
  102.  
  103. TToolsMenu::TToolsMenu()
  104. {
  105. }
  106.  
  107. //----------------------------------------------------------------------------------------
  108. // TToolsMenu::IToolsMenu
  109. //----------------------------------------------------------------------------------------
  110. #pragma segment AInit
  111.  
  112. void TToolsMenu::IToolsMenu(ResNumber menuID)
  113. {
  114.     this->ITearOffMenuView(menuID, kToolsPaletteWidth, kToolsPaletteHeight, gToolsPaletteWindow);
  115.  
  116.     TToolsPalette* aToolsPalette = new TToolsPalette;
  117.     aToolsPalette->IToolsPalette(NULL, this);
  118.     aToolsPalette->fIdentifier = 'MTUL';
  119. }
  120.  
  121. //========================================================================================
  122. // CLASS TToolsPalette
  123. //========================================================================================
  124. #undef Inherited
  125. #define Inherited TView
  126.  
  127. #pragma segment AOpen
  128. MA_DEFINE_CLASS_M1(TToolsPalette, Inherited);
  129.  
  130. //----------------------------------------------------------------------------------------
  131. // TToolsPalette Constructor
  132. //----------------------------------------------------------------------------------------
  133. #pragma segment AOpen
  134.  
  135. TToolsPalette::TToolsPalette()
  136. {
  137.     fCurrTool = 0;
  138.     fOldTool = 0;
  139.     fSelectedTool = 0;
  140.  
  141.     CRect r;
  142.     short top = 0;
  143.     for (short i=0; i <= kShapesInPalette; i++)        // Define the palette choices
  144.     {
  145.         SetRect(r, 0, top, kToolsPaletteWidth - 1, top + kToolsPaletteWidth - 1);
  146.         fChoiceArray[i] = r;
  147.         top += kToolsPaletteWidth - 1;
  148.     }
  149. }
  150.  
  151. //----------------------------------------------------------------------------------------
  152. // TToolsPalette::IToolsPalette
  153. //----------------------------------------------------------------------------------------
  154. #pragma segment AOpen
  155.  
  156. void TToolsPalette::IToolsPalette(TDocument* itsDocument, TView* itsSuperView)
  157. {
  158.     VPoint itsSize(kToolsPaletteWidth, kToolsPaletteHeight);
  159.     this->IView(itsDocument, itsSuperView, gZeroVPt, itsSize, sizeFixed, sizeFixed);
  160. }
  161.  
  162. //----------------------------------------------------------------------------------------
  163. // TToolsPalette::IToolsPalette
  164. //----------------------------------------------------------------------------------------
  165. #pragma segment ASelCommand
  166.  
  167. void TToolsPalette::DoMouseCommand(VPoint& theMouse,
  168.                                    TToolboxEvent* /*event*/,
  169.                                    CPoint /*hysteresis*/) // Override
  170. {
  171.     TToolsPalette* menuToolsPalette = NULL;
  172.     if (gToolsMenu)
  173.         menuToolsPalette = (TToolsPalette*)gToolsMenu->FindSubView('MTUL');
  174.  
  175.     TToolsPalette* floatingToolsPalette = NULL;
  176.     if (gToolsPaletteWindow)
  177.         floatingToolsPalette = (TToolsPalette*)gToolsPaletteWindow->FindSubView('TPLT');
  178.  
  179.     TToolSelectCmd* aToolSelectCmd = new TToolSelectCmd;
  180.     aToolSelectCmd->IToolSelectCmd(this, menuToolsPalette, floatingToolsPalette, theMouse);
  181.  
  182.     this->PostCommand(aToolSelectCmd);
  183. }
  184.  
  185. //----------------------------------------------------------------------------------------
  186. // TToolsPalette::TrackFeedback
  187. //----------------------------------------------------------------------------------------
  188. #pragma segment ASelCommand
  189.  
  190. void TToolsPalette::TrackFeedback(TrackPhase aTrackPhase,
  191.                                   const VPoint& /*anchorPoint*/,
  192.                                   const VPoint& /*previousPoint*/,
  193.                                   const VPoint& nextPoint,
  194.                                   Boolean mouseDidMove,
  195.                                   Boolean turnItOn)    // Override
  196. {
  197.     CPoint qdPt;
  198.     CRect  r;
  199.  
  200.     if (aTrackPhase == trackBegin)
  201.         fSelectedTool = kNoToolSelection;                // Start with no pre-selected tools
  202.  
  203.     if (mouseDidMove)
  204.     {
  205.         this->Focus();
  206.         qdPt = this->ViewToQDPt(nextPoint);
  207.         for (short i=0; i <= kShapesInPalette; i++)
  208.         {
  209.             r = fChoiceArray[i];
  210.             if (PtInRect(qdPt, r) && (fSelectedTool != i))
  211.             {
  212.                 this->FrameTool(r);
  213.                 if (turnItOn)
  214.                 {
  215.                     if (fSelectedTool != kNoToolSelection)
  216.                     {
  217.                         r = fChoiceArray[fSelectedTool];
  218.                         this->FrameTool(r);                // Turn off the current tool
  219.                     }
  220.                     fSelectedTool = i;                    // remember the Tool currently selected
  221.                 }
  222.                 else
  223.                     fSelectedTool = kNoToolSelection;    // no Tool currently selected
  224.                 break;
  225.             }
  226.         }
  227.     }
  228.  
  229.     if (aTrackPhase == trackEnd)
  230.     {
  231.         if (fSelectedTool != kNoToolSelection)
  232.         {
  233.             r = fChoiceArray[fSelectedTool];
  234.             this->FrameTool(r);
  235.         }
  236.     }
  237. }
  238.  
  239. //----------------------------------------------------------------------------------------
  240. // TToolsPalette::FrameTool
  241. //----------------------------------------------------------------------------------------
  242. #pragma segment ASelCommand
  243.  
  244. void TToolsPalette::FrameTool(CRect toolRect)
  245. {
  246.     PenNormal();
  247.     PenMode(patXor);
  248.     PenSize(2, 2);
  249.     FrameRect(toolRect);
  250. }
  251.  
  252. //----------------------------------------------------------------------------------------
  253. // TToolsPalette::DoSetCursor
  254. //----------------------------------------------------------------------------------------
  255. #pragma segment ARes
  256.  
  257. void TToolsPalette::DoSetCursor(const VPoint& /*localPoint*/, RgnHandle cursorRegion) // Override
  258. {
  259.     CRect qdExtent;
  260.  
  261.     SetCursor(&(qd.arrow));
  262.     this->GetQDExtent(qdExtent);
  263.     RectRgn(cursorRegion, qdExtent);
  264. }
  265.  
  266. //----------------------------------------------------------------------------------------
  267. // TToolsPalette::Draw
  268. //----------------------------------------------------------------------------------------
  269. #pragma segment ARes
  270.  
  271. void TToolsPalette::Draw(const VRect& area) // Override
  272. {
  273.     CRect r;
  274.  
  275.     PenSize(1, 1);
  276.     MoveTo((short)fSize.h - 1, 0);
  277.     Line(0, (short)fSize.v);
  278.     for (short i=0; i <= kShapesInPalette; i++)
  279.     {
  280.         r = fChoiceArray[i];
  281.         FrameRect(r);
  282.         TShape* shape = GetShapeInArray(i);
  283.         if (shape)
  284.             shape->Draw();
  285.     }
  286.  
  287.     if (fCurrTool != kNoToolSelection)
  288.         this->Toggle(fCurrTool);
  289.  
  290.     Inherited::Draw(area);
  291. }
  292.  
  293. //----------------------------------------------------------------------------------------
  294. // TToolsPalette::SelectNewTool
  295. //----------------------------------------------------------------------------------------
  296. #pragma segment ShapeRes
  297.  
  298. void TToolsPalette::SelectNewTool(short whichTool)
  299. {
  300.     if (this->IsShown())
  301.     {
  302.         this->Focus();
  303.         this->Toggle(fCurrTool);
  304.         fCurrTool = whichTool;
  305.         this->Toggle(fCurrTool);
  306.     }
  307.     else
  308.         fCurrTool = whichTool;    // Just set the field so that it is correct when it is shown
  309. }
  310.  
  311. //----------------------------------------------------------------------------------------
  312. // TToolsPalette::Toggle
  313. //----------------------------------------------------------------------------------------
  314. #pragma segment ShapeRes
  315.  
  316. void TToolsPalette::Toggle(short theTool)
  317. {
  318.     CRect r = fChoiceArray[theTool];
  319.     UseSelectionColor();
  320.     InvertRect(r);
  321. }
  322.  
  323. //========================================================================================
  324. // CLASS TToolSelectCmd
  325. //========================================================================================
  326. #undef Inherited
  327. #define Inherited TTearOffMenuViewTracker
  328.  
  329. #pragma segment AInit
  330. MA_DEFINE_CLASS_M1(TToolSelectCmd, Inherited);
  331.  
  332. //----------------------------------------------------------------------------------------
  333. // TToolSelectCmd Constructor
  334. //----------------------------------------------------------------------------------------
  335. #pragma segment AInit
  336.  
  337. TToolSelectCmd::TToolSelectCmd()
  338. {
  339.     fMenuToolsPalette = NULL;
  340.     fFloatingToolsPalette = NULL;
  341.     fTool = kNoToolSelection;
  342.     fExitTracking = false;
  343. }
  344.  
  345. //----------------------------------------------------------------------------------------
  346. // TToolSelectCmd::IToolSelectCmd
  347. //----------------------------------------------------------------------------------------
  348. #pragma segment ASelCommand
  349.  
  350. void TToolSelectCmd::IToolSelectCmd(TToolsPalette* theToolsPalette,
  351.                                     TToolsPalette* theMenuToolsPalette,
  352.                                     TToolsPalette* theFloatingToolsPalette,
  353.                                     VPoint theMouse)
  354. {
  355.     this->ITearOffMenuViewTracker(cChangeTool, NULL, kCantUndo, kDoesNotCauseChange,
  356.                                   NULL, theToolsPalette, NULL, theMouse);
  357.  
  358.     fMenuToolsPalette = theMenuToolsPalette;
  359.     fFloatingToolsPalette = theFloatingToolsPalette;
  360.     fViewConstrain = false;
  361. }
  362.  
  363. //----------------------------------------------------------------------------------------
  364. // TToolSelectCmd::TrackMouse
  365. //----------------------------------------------------------------------------------------
  366. #pragma segment ADoCommand
  367.  
  368. TTracker* TToolSelectCmd::TrackMouse(TrackPhase aTrackPhase,
  369.                                      VPoint& /*anchorPoint*/,
  370.                                      VPoint& /*previousPoint*/,
  371.                                      VPoint& nextPoint,
  372.                                      Boolean /*mouseDidMove*/)    // Override
  373. {
  374.     TTracker* newTracker = this;
  375.     if (gTrackingInMenu)
  376.     {
  377.         // quit tracking when the mouse leaves the view
  378.         // - if gTrackingInMenu is TRUE, view constrain is set to FALSE, so nextPoint CAN leave the view
  379.         // - all other times, view constrain is TRUE, so nextPoint WON'T leave the view
  380.  
  381.         if (fView && fView->IsShown())
  382.             fExitTracking = !fView->ContainsMouse(nextPoint);
  383.  
  384.         // if gTrackingInMenu is TRUE, then fExitTracking might get set to TRUE above, meaning
  385.         // that the user is either returning to the menubar or trying to tear off this menu, so
  386.         // simply return NULL when we're done tracking and the tracker will be freed by
  387.         // TApplication::TrackMouse
  388.  
  389.         if (aTrackPhase == trackRelease && fExitTracking)
  390.             newTracker = NULL;
  391.     }
  392.  
  393.     return newTracker;
  394. }
  395.  
  396. //----------------------------------------------------------------------------------------
  397. // TToolSelectCmd::TrackFeedback
  398. //----------------------------------------------------------------------------------------
  399. #pragma segment ADoCommand
  400.  
  401. void TToolSelectCmd::TrackFeedback(TrackPhase aTrackPhase,
  402.                                    const VPoint& anchorPoint,
  403.                                    const VPoint& previousPoint,
  404.                                    const VPoint& nextPoint,
  405.                                    Boolean mouseDidMove,
  406.                                    Boolean turnItOn)    // Override
  407. {
  408.     // All we really want to do here is let the view give us some feedback, so...
  409.     if (fView && fView->IsShown() && mouseDidMove)
  410.         fView->TrackFeedback(aTrackPhase, anchorPoint, previousPoint, nextPoint, mouseDidMove, turnItOn);
  411.  
  412.     // When we're selecting a tool (i.e. turning it on, then this command needs to know
  413.     // about the new selection. When we're deselecting a tool (i.e. turning it off) then this
  414.     // command MUST ignore the deselection since MacApp's tracking will turn off the current
  415.     // selection as part of its default tracking behavior even when the user has made a valid
  416.     // selection in the view.
  417.     if (turnItOn)
  418.         fTool = ((TToolsPalette*)fView)->GetSelectedTool();
  419. }
  420.  
  421. //----------------------------------------------------------------------------------------
  422. // TToolSelectCmd::IsDoneTracking
  423. //----------------------------------------------------------------------------------------
  424. #pragma segment ADoCommand
  425.  
  426. Boolean TToolSelectCmd::IsDoneTracking()    // Override
  427. {
  428.     return (!StillDown() || fExitTracking);
  429. }
  430.  
  431. //----------------------------------------------------------------------------------------
  432. // TToolSelectCmd::DoIt
  433. //----------------------------------------------------------------------------------------
  434. #pragma segment ADoCommand
  435.  
  436. void TToolSelectCmd::DoIt()    // Override
  437. {
  438.     // Tell the TToolsPalette in the menu which tool is selected
  439.     if (fMenuToolsPalette)
  440.     {
  441.         fMenuToolsPalette->fOldTool = fMenuToolsPalette->fCurrTool;
  442.         fMenuToolsPalette->fCurrTool = fTool;
  443.     }
  444.  
  445.     // Tell the TToolsPalette in the floating window which tool is selected
  446.     if (fFloatingToolsPalette)
  447.     {
  448.         fFloatingToolsPalette->fOldTool = fFloatingToolsPalette->fCurrTool;
  449.         fFloatingToolsPalette->SelectNewTool(fTool);
  450.     }
  451. }
  452.  
  453. //----------------------------------------------------------------------------------------
  454. // TToolSelectCmd::RedoIt
  455. //----------------------------------------------------------------------------------------
  456. #pragma segment ADoCommand
  457.  
  458. void TToolSelectCmd::RedoIt()    // Override
  459. {
  460.     // Tell the TToolsPalette in the menu which tool is selected
  461.     if (fMenuToolsPalette)
  462.         fMenuToolsPalette->fCurrTool = fTool;
  463.  
  464.     // Tell the TToolsPalette in the floating window which tool is selected
  465.     if (fFloatingToolsPalette)
  466.         fFloatingToolsPalette->SelectNewTool(fTool);
  467. }
  468.  
  469. //----------------------------------------------------------------------------------------
  470. // TToolSelectCmd::UndoIt
  471. //----------------------------------------------------------------------------------------
  472. #pragma segment ADoCommand
  473.  
  474. void TToolSelectCmd::UndoIt()    // Override
  475. {
  476.     // Tell the TToolsPalette in the menu which tool *was* selected
  477.     if (fMenuToolsPalette)
  478.         fMenuToolsPalette->fCurrTool = fMenuToolsPalette->fOldTool;
  479.  
  480.     // Tell the TToolsPalette in the floating window which tool *was* selected
  481.     if (fFloatingToolsPalette)
  482.         fFloatingToolsPalette->SelectNewTool(fFloatingToolsPalette->fOldTool);
  483. }
  484.  
  485. //========================================================================================
  486. // GLOBAL Functions
  487. //========================================================================================
  488. #undef Inherited
  489.  
  490. //----------------------------------------------------------------------------------------
  491. // GetToolsPalette
  492. //----------------------------------------------------------------------------------------
  493. #pragma segment ShapeRes
  494.  
  495. TToolsPalette* GetToolsPalette()
  496. {
  497.     return (TToolsPalette*) gToolsMenu->FindSubView('MTUL');
  498. }
  499.  
  500.